package com.guli.mall.pms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.mall.pms.entity.SpuImagesEntity;
import com.guli.mall.pms.service.SkuImagesService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.pms.mapper.SpuInfoDescMapper;
import com.guli.mall.pms.entity.SpuInfoDescEntity;
import com.guli.mall.pms.service.SpuInfoDescService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("spuInfoDescService")
public class SpuInfoDescServiceImpl extends ServiceImpl<SpuInfoDescMapper, SpuInfoDescEntity> implements SpuInfoDescService {

    @Resource
    SpuInfoDescMapper spuInfoDescMapper;

    @Resource
    SkuImagesService skuImagesService;

    @Override
    public PageUtils queryPage(Integer currentPage,Integer size) {

        Page<SpuInfoDescEntity> page = new Page<>(currentPage,size);

        Page<SpuInfoDescEntity> resultPage = this.page(page);


        return new PageUtils(resultPage);
    }

    /**
     * 携带id一起插入，因为默认的插入方式在插入的时候会忽略id
     * @param spuInfoDescEntity
     */
    @Override
    public void insertAndId(SpuInfoDescEntity spuInfoDescEntity) {

        spuInfoDescMapper.insertAndId(spuInfoDescEntity);
    }

    @Override
    public List<SpuImagesEntity> getBySpuIdAndAddImgPrefixAndEncapsulateToSpuImagesEntityList(Long spuId) {

        SpuInfoDescEntity spuInfoDesc = this.getById(spuId);

        if(!ObjectUtils.isEmpty(spuInfoDesc)&&!StringUtils.isEmpty(spuInfoDesc.getDecript())){


            return Arrays.stream(spuInfoDesc.getDecript().split(",")).map(imgPostfix -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();

                spuImagesEntity.setImgUrl(skuImagesService.getImagePrefix() + imgPostfix);

                return spuImagesEntity;
            }).collect(Collectors.toList());


        }

        return null;
    }

}